﻿<?php
require_once("page.class.php");
require_once("adresaTemplate_string.php");

class radnik extends page{
	var $db;
	var $adresa_object;
	var $_configArej;
	
	var $picturesPath = "pictures/radnici/";
	
	function radnik($get,$post=""){
		require_once("db_functions.php");
		$this->db = new db_functions();
		$this->loadConfig();
		$this->adresa_object = new adrese();
		
		$action = strlen($get["a"])?$get["a"]:$post["a"];
		switch($action){
			case 'new':
				$this->displayNewRadnik();
				break;
			case 'editRadnik':
				$this->editRadnik($get["id"],$get["cp"],$get["act"],$get["ime"],$get["tel"]);
				break;
			case 'deleteRadnik':
				$ok = $this->deleteRadnik($get["id"]);
				if($ok){
					echo "Radnik ".$get["ime"]." uspešno obrisan!";
				}else{
					echo mysql_error();
				}
				break;
			case 'deleteMore':
				$ids = explode("{!}",$get["ids"]);
				foreach ($ids as $id){
					$ok = $this->deleteRadnik($id);
					if(!$ok){
						die(mysql_error());
						break;
					}
				}
				echo "Uspešno obrisano ".count($ids)." radnika.";
				break;
			case 'toggleAct':
				$ids = explode("{!}",$get["ids"]);
				$this->toggleActivity($ids);
				break;
			case 'pop':
				$q = "SELECT radnik_id FROM radnici WHERE concat(radnik_ime,' ',radnik_prezime)='".$get["name"]."'";
				$res = $this->db->select2($q);
				echo $get["id"].": ".$get["name"]."\nIz baze: ".$res[0]["radnik_id"];
				break;
			case 'save':
				$this->saveRadnik($post);
				break;
			case 'add':
				//print_r($get);
				$this->addNewRadnik($get);
				break;
			case 'alarms':
				$this->alarms();
				break;
			default:
				$filter = array(
					"a" => trim($get["akt"]),
					"ime"=> trim($get["ime"]),
					"tel" => trim($get["tel"]),
					"tip" => trim($get["tip"])
					
				);
				$this->browse($get["p"],$filter);
		}
	}
	
	function alarms(){
		$day_sec = 60*60*24;
		
		$start_date = trim(getFromInput("start_date"));
		$end_date = trim(getFromInput("end_date"));
		
		if (!preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$start_date)) {
			$start_date = date("d.m.Y");
		}
		
		if (!preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$end_date)) {
			$sint = date2int($start_date);
			$end_date = date("d.m.Y",$sint+(60*60*24));
		}
		
		$this->assign("start_date",$start_date);
		$this->assign("end_date",$end_date);
		
		$start_time = date2int($start_date);
		$start_day = date("d",$start_time);
		$start_month = date("m",$start_time);
		$start_year = date("Y",$start_time);
		
		$end_time = date2int($end_date);
		$end_day = date("d",$end_time);
		$end_month = date("m",$end_time);
		$end_year = date("Y",$end_time);
		
		$year = date("Y");
		
		if ($start_time<=$end_time) {
			
			
			// RODJENDANI
			$q = "SELECT radnik_id, radnik_ime, radnik_prezime, radnik_rodjendan "
				."FROM radnici WHERE (radnik_akt=1) AND (0";
				$time = $start_time;
				
				while ($time<=$end_time) {
					$d = date("d",$time);
					$m = date("m",$time);
					$q .= " OR (radnik_rodjendan LIKE '%-$m-$d')";
					$time += $day_sec;
				}
			
			$q .= ") ORDER BY radnik_rodjendan, radnik_ime, radnik_prezime";
			
			$res=$this->db->select2($q);
			if ($res !== false) {
				foreach ($res as $row){
					$rodj_ids[]		= $row["radnik_id"];
					$rodj_ime[]		= $row["radnik_ime"];
					$rodj_prezime[]	= $row["radnik_prezime"];
					$rodj_date[]	= datetime2str($row["radnik_rodjendan"]);
					$rodj_year[] = $year-intval(substr($row["radnik_rodjendan"],0,4));
				}
				$this->assign("rodj_id",$rodj_ids);
				$this->assign("rodj_ime",$rodj_ime);
				$this->assign("rodj_prezime",$rodj_prezime);
				$this->assign("rodj_date",$rodj_date);
				$this->assign("rodj_year",$rodj_year);
			}else{
				$this->assign("comment_rodj_start","<!--");
				$this->assign("comment_rodj_end","--><p>Nema rođendana.</p>");
			}
			
			// UGOVORI
			//$today_time = makeUnixTime(0,0,0,$month,$today,$year);
			//$tomorow_time = makeUnixTime(23,59,59,$month,$tomorow,$year);
			$q = "SELECT radnik_id, radnik_ime, radnik_prezime, radnik_ugo_start, radnik_ugo_end "
				."FROM radnici "
				."WHERE radnik_akt=1 "
				."AND radnik_ugo_end>=$start_time "
				."AND radnik_ugo_end<".($end_time+$day_sec)." "
				."ORDER BY radnik_ugo_end";
			$res=$this->db->select2($q);
			if ($res !== false) {
				foreach ($res as $row){
					$ugo_ids[]		= $row["radnik_id"];
					$ugo_ime[]		= $row["radnik_ime"];
					$ugo_prezime[]	= $row["radnik_prezime"];
					$ugo_start[]	= date("d.m.Y",$row["radnik_ugo_start"]);
					$ugo_end[]	= date("d.m.Y",$row["radnik_ugo_end"]);
				}
				$this->assign("ugo_id",$ugo_ids);
				$this->assign("ugo_ime",$ugo_ime);
				$this->assign("ugo_prezime",$ugo_prezime);
				$this->assign("ugo_start",$ugo_start);
				$this->assign("ugo_end",$ugo_end);
			}else{
				$this->assign("comment_ugo_start","<!--");
				$this->assign("comment_ugo_end","--><p>Nema isticanja ugovora.</p>");
			}
		}else{
			$this->assign("comment_ugo_start","<!--");
			$this->assign("comment_ugo_end","--><p>Datum DO je veći od datuma OD</p>");
			$this->assign("comment_rodj_start","<!--");
			$this->assign("comment_rodj_end","--><p>Datum DO je veći od datuma OD</p>");
		}
		
		echo $this->parseTemplate("radnici/alarms.tpl");
	}
	//--------------------------------------------
	/**
	 * preuzima sve banke
	 * @return Array()
	 *
	 */
	function get_banke(){
		$ret = array();
		$q = "SELECT banka_id, banka_name FROM banke ORDER BY banka_name";
			
		$res= $this->db->select2($q);
		if(count($res)>0){
			foreach($res as $key_res=>$val_res){
				$res['polje_id'][$key_res]=$val_res["banka_id"];
				$res['polje_ime'][$key_res]=$val_res["banka_name"];
			}
		}
		//print_r($del_id);
		return $res;
	}
	//--------------------------------------------
	/**
	 * preuzima listu tipova radnika
	 * @return Array()
	 */
	function get_Radnici_Tipovi($tpid=null){
		$ret = array();
		$q = "SELECT rt.* FROM radnici_tipovi rt ";	
		$q .= " ORDER BY rt.rad_tip_name";
			
		$res= $this->db->select2($q);
		//echo "".$q."<hr/>".$this->page_max;
		if(count($res)>0){
			//print_r($res);
			foreach($res as $key_res=>$val_res){
				//ime na Engleskom		
				if($val_res["rad_tip_id"])
					$res['polje_id'][$key_res]=$val_res["rad_tip_id"];
				else 
					$res['polje_id'][$key_res]="0 ";
					
				if($val_res["rad_tip_name"])
					$res['polje_ime'][$key_res]=$val_res["rad_tip_name"];
				else
					$res['polje_ime'][$key_res]="&nbsp;";
				if ($val_res["rad_tip_id"]==$tpid) {
					$res['polje_selected'][$key_res]=" selected";
				}else{
					$res['polje_selected'][$key_res]="";
				}
			}
		}
		//print_r($del_id);
		return $res;
	}
	//--------------------------------------------
	/**
	 * preuzima listu strucne spreme radnika
	 * @return Array()
	 */
	function get_Radnici_Sprema(){
		$ret = array();
		$q = "SELECT rs.* FROM radnici_sprema rs ";	
		$q .= " ORDER BY rs.rad_ss_name";
			
		$res= $this->db->select2($q);
		//echo "".$q."<hr/>".$this->page_max;
		if(count($res)>0){
			//print_r($res);
			foreach($res as $key_res=>$val_res){
				//ime na Engleskom
				if($val_res["rad_ss_id"])
					$res['polje_id'][$key_res]=$val_res["rad_ss_id"];
				else
					$res['polje_id'][$key_res]="0 ";
					
				if($val_res["rad_ss_name"])
					$res['polje_ime'][$key_res]=$val_res["rad_ss_name"];
				else 
					$res['polje_ime'][$key_res]="&nbsp;";
				
				if($val_res["rad_ss_osn"])
					$res['polje_osnovica'][$key_res]=$val_res["rad_ss_osn"];
				else 
					$res['polje_osnovica'][$key_res]="0 ";
			}
		}
		//print_r($del_id);
		return $res;
	}
	
	//--------------------------------------------
	/**
	 * preuzima listu grupe radnika
	 * @return Array()
	 */
	function get_Radnici_Grupa(){
		$ret = array();
		$q = "SELECT rg.* FROM radnici_grupe rg ";	
		$q .= " ORDER BY rg.rad_grupa_name";
			
		$res= $this->db->select2($q);
		//echo "".$q."<hr/>".$this->page_max;
		if(count($res)>0){
			//print_r($res);
			foreach($res as $key_res=>$val_res){
				if ($val_res["rad_grupa_id"])
					$res['polje_id'][$key_res]=$val_res["rad_grupa_id"];
				else 
					$res['polje_id'][$key_res]="0 ";
				
				if($val_res["rad_grupa_name"])
					$res['polje_ime'][$key_res]=$val_res["rad_grupa_name"];
				else 
					$res['polje_ime'][$key_res]="&nbsp;";
			}
		}
		//print_r($del_id);
		return $res;
	}
	
	//--------------------------------------------
	/**
	 * preuzima listu radnih odnosa radnika
	 * @return Array()
	 */
	function get_Radnici_RadniOdnos(){
		$ret = array();
		$q = "SELECT ro.* FROM radnici_ro ro ";	
		$q .= " ORDER BY ro.rad_ro_name";
			
		$res= $this->db->select2($q);
		//echo "".$q."<hr/>".$this->page_max;
		if(count($res)>0){
			//print_r($res);
			foreach($res as $key_res=>$val_res){
				if($val_res["rad_ro_id"])
					$res['polje_id'][$key_res]=$val_res["rad_ro_id"];
				else 
					$res['polje_id'][$key_res]="0 ";
				
				if($val_res["rad_ro_sifra"])
					$res['polje_sifra'][$key_res]=$val_res["rad_ro_sifra"];
				else
					$res['polje_sifra'][$key_res]="0 ";
					
				if($val_res["rad_ro_name"])
					$res['polje_ime'][$key_res]=$val_res["rad_ro_name"];
				else 
					$res['polje_ime'][$key_res]="&nbsp;";
			}
		}
		//print_r($del_id);
		return $res;
	}
	//--------------------------------------------
	/**
	 * dodaje novu adresu u bazu, i vraca ID polja koje je dodato
	 * @param $polje_naziv
	 * @param Array $get
	 */
	function saveAdresa($polje_naziv, $get){
		
		if($polje_naziv){
			//prvo proverimo da li vec postoji ono sta bi uneli
			$sql="SELECT COUNT(*) FROM adrese WHERE ( adresa_ul_id= '".$get['adresa_ul_id']."' AND  adresa_lid='".$get['adresa_lid']."' AND  adresa_broj='".$get['adresa_broj']."' ) ";
			/*
			adresa_id 
			adresa_ul_id
			adresa_lid
			adresa_broj
			*/
			//echo $sql;
			$ima=$this->db->getOne($sql);
			//echo $ima;
			//samo ako nema takvih unetih kategorija, unosimo novu
			if(!$ima){
				
				//upis kategorije
				$sql="INSERT INTO adrese (adresa_ul_id, adresa_lid, adresa_broj) VALUES('$polje_name','$polje_name','$polje_name'); ";
				//echo $sql;
				if($this->db->alterTable($sql)){
					$sql="SELECT adresa_id FROM adrese WHERE ( adresa_ul_id= '".$get['adresa_ul_id']."' AND  adresa_lid='".$get['adresa_lid']."' AND  adresa_broj='".$get['adresa_broj']."' ) ";
					$ima=$this->db->getOne($sql);
					return $ima;
				}
				else{
					//neuspesan unos
					return -1;
				}
			}//of if(ima)
			else{
				//polje vec postoji
				return 0;
			}
				
		}
		
	}
	
	
	
	//--------------------------------------------
	/**
	 * dodaje novo polje o radniku
	 * @param Array $get
	 */
	function addNewRadnik($get){
		
		//uzimamo vrednost ID-a sacuvane adrese u dB
		$adresa_lk = saveAdresa("lk", $get);
		$adresa_st = saveAdresa("st", $get);
		
		if($polje_name){
				//prvo proverimo da li vec postoji ono sta bi uneli
				$sql="SELECT COUNT(*) FROM radnici WHERE ( radnik_gr_id= '".$get['radnik_gr_id']."' AND radnik_tip_id= '".$get['radnik_tip_id']."' AND radnik_ssprema_id= '".$get['radnik_ssprema_id']."' AND radnik_rodnos= '".$get['radnik_rodnos']."' AND radnik_ime= '".$get['radnik_ime']."' AND radnik_prezime= '".$get['radnik_prezime']."' AND radnik_tel= '".$get['radnik_tel']."' AND radnik_mob= '".$get['radnik_mob']."' AND radnik_adresa_id= '".$adresa_st."' AND radnik_adresa_lk= '".$adresa_lk."' AND radnik_card_id= '".$get['radnik_card_id']."' AND radnik_jmbg= '".$get['radnik_jmbg']."' AND radnik_rodjendan= '".$get['radnik_rodjendan']."' AND radnik_rodjendan= '".$get['radnik_rodjendan']."' AND radnik_broj_lk= '".$get['radnik_broj_lk']."' AND radnik_ziroracun= '".$get['radnik_ziroracun']."' AND radnik_komentar= '".$get['radnik_komentar']."' ) ";
				/*
	radnik_id 			
	radnik_gr_id
	radnik_tip_id
	radnik_ssprema_id
	radnik_rodnos
	radnik_ime
	radnik_prezime
	radnik_tel
	radnik_mob
	radnik_adresa_id
	radnik_adresa_lk
	radnik_card_id
	radnik_jmbg
	radnik_rodjendan
	radnik_broj_lk
	radnik_ziroracun
	radnik_komentar 
				*/
				//echo $sql;
				$ima=$this->db->getOne($sql);
				//echo $ima;
				//samo ako nema takvih unetih kategorija, unosimo novu
				if(!$ima){
					
				//upis kategorije
				$sql="INSERT INTO radnici ( radnik_gr_id, radnik_tip_id, radnik_ssprema_id, radnik_rodnos, radnik_ime, radnik_prezime, radnik_tel, radnik_mob, radnik_adresa_id, radnik_adresa_lk, radnik_card_id, radnik_jmbg, radnik_rodjendan, radnik_broj_lk, radnik_ziroracun, radnik_komentar ) VALUES('$polje_name'); ";
				//echo $sql;
				if($this->db->alterTable($sql)){
					$alert="<script>alert('Uspešno dodato polje : $polje_name ');</script>";
					echo $alert;
				}
				}//of if(ima)
				else{
					echo "<script>alert('Pojam koji unosite već postoji!');</script>";
				}
				
			}
	}
	//--------------------------------------------
	function displayNewRadnik(){
		// banke
		$polja_lista=$this->get_banke();
			$this->assign("banka_id",$polja_lista['polje_id']);
			$this->assign("banka_name",$polja_lista['polje_ime']);
			$this->assign("sve_banke",$this->parseTemplate("sve_banke_select.tpl"));
			$this->assign("comment_start_remove_banka","<!--");
			$this->assign("comment_end_remove_banka","-->");
			
		// adresa
		$polja_lista = $this->get_Regioni();
			$this->assign("region_id",$polja_lista["polje_id"]);
			$this->assign("region_name",$polja_lista["polje_ime"]);
		$polja_lista = $this->get_Opstine();
			$this->assign("opstina_id",$polja_lista["polje_id"]);
			$this->assign("opstina_name",$polja_lista["polje_ime"]);
			$this->assign("opstina_parent",$polja_lista["polje_parent"]);
		$polja_lista = $this->get_Mesta();
			$this->assign("grad_id",$polja_lista["polje_id"]);
			$this->assign("grad_name",$polja_lista["polje_ime"]);
			$this->assign("grad_parent",$polja_lista["polje_parent"]);
			$this->assign("grad_ptt",$polja_lista["polje_ptt"]);
		$polja_lista = $this->get_LokOpstine();
			$this->assign("lokopstina_id",$polja_lista["polje_id"]);
			$this->assign("lokopstina_name",$polja_lista["polje_ime"]);
			$this->assign("lokopstina_parent",$polja_lista["polje_parent"]);
			$this->assign("lokopstina_ptt",$polja_lista["polje_ptt"]);
		$polja_lista = $this->get_Naselja();
			$this->assign("naselje_id",$polja_lista["polje_id"]);
			$this->assign("naselje_name",$polja_lista["polje_ime"]);
			$this->assign("naselje_parent",$polja_lista["polje_parent"]);
			$this->assign("naselje_ptt",$polja_lista["polje_ptt"]);
		
			
		$this->assign("search_ul_type","1");

		$this->assign("new_region","region_radnik");
		$this->assign("new_opstina","opstina_radnik");
		$this->assign("new_grad","grad_radnik");
		$this->assign("new_lokopstina","lok_opstina_radnik");
		$this->assign("new_naselje","naselje_radnik");
		$this->assign("new_ulica","ulica_radnik");
		$this->assign("new_ulica_id","ulica_id_radnik");
		$this->assign("new_broj","broj_radnik");	
		$this->assign("adresa",$this->parseTemplate("adresa/new.tpl"));
		
		$this->assign("new_region","region_radnik_lk");
		$this->assign("new_opstina","opstina_radnik_lk");
		$this->assign("new_grad","grad_radnik_lk");
		$this->assign("new_lokopstina","lok_opstina_radnik_lk");
		$this->assign("new_naselje","naselje_radnik_lk");
		$this->assign("new_ulica","ulica_radnik_lk");
		$this->assign("new_ulica_id","ulica_id_radnik_lk");
		$this->assign("new_broj","broj_radnik_lk");
		$this->assign("adresa_lk",$this->parseTemplate("adresa/new.tpl"));
		
		$polja_lista=$this->get_Radnici_Grupa();
			$this->assign("grupa_id",$polja_lista['polje_id']);
			$this->assign("grupa_name",$polja_lista['polje_ime']);	
			
		$polja_lista=$this->get_Radnici_Sprema();
			$this->assign("ss_id",$polja_lista['polje_id']);
			$this->assign("ss_name",$polja_lista['polje_ime']);
		
		$polja_lista=$this->get_Radnici_RadniOdnos();
			$this->assign("ro_id",$polja_lista['polje_id']);
			$this->assign("ro_name",$polja_lista['polje_ime']);
			
		$polja_lista=$this->get_Radnici_Tipovi();
			$this->assign("tip_id",$polja_lista['polje_id']);
			$this->assign("tip_name",$polja_lista['polje_ime']);
			
		
		$adresa_lk= getAdresaHtml(-1, "", "_lk");
			$this->assign("adresa_dokumenti",$adresa_lk);
		$adresa_lk= getAdresaHtml(-1, "", "_st");	
			$this->assign("adresa_stanovanje",$adresa_lk);	
			//$this->assign("probni_tekst",$adresa_lk);
		
		echo $this->parseTemplate("radnici/new.tpl");
	}
	//--------------------------------------------
	function browse($page,$filter){
		$level = $this->getUserLevel();
		if(strtolower($level["name"]) != "admin"){
			$this->assign("comment_start_user","<!--");
			$this->assign("comment_end_user","-->");
		}
		
		$polja = $this->get_Radnici_Tipovi($filter["tip"]);
			$this->assign("radnik_tip_id",$polja["polje_id"]);
			$this->assign("radnik_tip_name",$polja["polje_ime"]);
			$this->assign("radnik_tip_selected",$polja["polje_selected"]);
		
		//print_r($filter);
		$sql = "SELECT radnik_id, radnik_akt,radnik_ime,radnik_prezime,radnik_card_id,radnik_rodjendan,radnik_ugo_start, radnik_ugo_end "
			  ."radnik_komentar,radnik_adresa_id,rad_grupa_name,radnik_tel "
			  ."FROM radnici,radnici_grupe ";
		
		if(strlen($filter["tel"])){
			$sql .= ",radnici_telefoni ";
		}
			  
		$sql .= "WHERE radnici.radnik_gr_id=radnici_grupe.rad_grupa_id ";
		
		if(strlen($filter["tel"])){
			$sql .= "AND radnici.radnik_id=radnici_telefoni.rad_id AND radnici_telefoni.value LIKE '%".$filter["tel"]."%' ";
		}
		
		if(strlen($filter["ime"])){
			$sql .= " AND (CONCAT(radnik_ime,' ',radnik_prezime) LIKE '%".$filter["ime"]."%' OR radnik_id='".$filter["ime"]."')";
		}
		if ($filter["tip"]>0) {
			$sql .= " AND radnik_tip_id=".$filter["tip"]." ";
		}
		
		$this->assign("filter_tip",$filter["tip"]);
		$this->assign("filter_radnik",$filter["ime"]." ");
		$this->assign("filter_tel",$filter["tel"]." ");
		
		switch ($filter["a"]){
			case 1:
				$this->assign("selected_fa_1"," selected=\"selected\"");
				$sql .= "AND radnik_akt=1 ";
				break;
			case 2:
				$this->assign("selected_fa_0"," selected=\"selected\"");
				$sql .= "AND radnik_akt=0 ";
				break;
			default:
				$filter["a"] = "0 ";
				break;
		}
		
		$sql .= "ORDER BY concat(radnici.radnik_ime,' ',radnici.radnik_prezime) ";
		
		$max_page = 15;
		
		if (!$page) {
			$page = 1;
		}
		
		$ukupno = count($this->db->select1($sql));
		$base = bcdiv($ukupno,$max_page,0);
		$rest = $ukupno%$max_page;
		$num_pages = $rest>0 ? $base+1 : $base;
		
		if($num_pages > 1){
			for ($i=1; $i<=$num_pages; $i++){
				$pages[] = $i;
				if($i == $page){
					$current_page[] = ' id="currentPage"';
				}else{
					$current_page[] = "";
				}
			}
			$this->assign("page",$pages);
			$this->assign("current_page",$current_page);
		}else{
			$this->assign("pages_start_comment","<br /><br /><!--");
			$this->assign("pages_end_comment","-->");
		}
		
		$start = ($page-1)*$max_page;
		
		$this->assign("debug",$sql);
		$workers = $this->db->select2($sql);
		if($workers !== false){
			$this->assign("info","Ukupno: ".count($workers));
			
			$sql .= "LIMIT $start, $max_page";
			$workers = $this->db->select2($sql);
			foreach($workers as $v){
				$akt[]		= ($v["radnik_akt"]==0)?"color:#AAA":"";
				$id[]		= ($v["radnik_id"])? $v["radnik_id"]: " 0 ";
				$card_id[]	= ($v["radnik_card_id"])? $v["radnik_card_id"]:" 0 ";
				$ugo_start[]= $v["radnik_ugo_start"]>0?date("d.m.Y",$v["radnik_ugo_start"]):"";
				$ugo_end[]	= $v["radnik_ugo_end"]>0?date("d.m.Y",$v["radnik_ugo_end"]):"";
				$first[]	= $v["radnik_ime"];
				$last[]	 	= $v["radnik_prezime"];
				$jmbg[]		= $v["radnik_jmbg"];
				$group[]	= $v["rad_grupa_name"];
				$ro_sifra[]	= $v["rad_ro_sifra"];
				$ro_name[]	= $v["rad_ro_name"];
				$ss[]		= $v["rad_ss_name"];
				$type[]		= $v["rad_tip_name"];
				if(strlen($v["radnik_komentar"])){
					$kom_max = 30;
					if(strlen($v["radnik_komentar"]) > $kom_max){
						$comm = nl2br(substr($v["radnik_komentar"],0,$kom_max-3))."...";
					}else{
						$comm = nl2br($v["radnik_komentar"]);
					}
					$comment[]	= $comm;
				}else{
					$comment[] = "";
				}
				// telefoni
				$ph = "";
				$tbl_tels = $this->db->select2("SELECT * FROM radnici_telefoni WHERE rad_id=".$v["radnik_id"]." ORDER BY value");
				if($tbl_tels[0]){
					foreach ($tbl_tels as $tel){
						if($tel["value"])
						$ph .= $tel["value"]."<br />";
					}
					$ph = substr($ph,0,strlen($ph)-6);
				}
				$phone[] = $ph;
				
				$tmp = $v["radnik_ziroracun"];
				$tmp1 = substr($tmp,0,3);
				$tmp2 = substr($tmp,3,strlen($tmp)-2);
				$tmp3 = substr($tmp,strlen($tmp)-2,2);
				$zr[]		= $tmp1."-".$tmp2."-".$tmp3;
				
				if($v["radnik_rodjendan"]){
					
					/*$b	= @date($this->getFromConfig("short_date"),$v["radnik_rodjendan"]);
					$tmp = explode(".",$b,3);
					$months = $this->getFromConfig("month_names");
					$birth[]	= $tmp[0].". ".$months[$tmp[1]-1]." ".$tmp[2];*/
					$birth[] = datetime2str($v["radnik_rodjendan"]);
				}else{
					$birth[] = "&nbsp;";
				}
				
				$adr = $v["radnik_adresa_id"];
				if($adr){
					$adr = $this->adresa_object->getAll($adr);
					$address[] = $adr["ulica"]." ".$adr["broj"].", ".$adr["lokacija"]["mesto"]["name"];
				}else{
					$address[] = "&nbsp;";
				}
			}
		}
		$this->assign("akt_style",$akt);
		$this->assign("active",$filter["a"]);
		$this->assign("id",$id);
		$this->assign("card_id",$card_id);
		$this->assign("first",$first);
		$this->assign("last",$last);
		$this->assign("address",$address);
		$this->assign("jmbg",$jmbg);
		$this->assign("group",$group);
		$this->assign("ro_sifra",$ro_sifra);
		$this->assign("ro_name",$ro_name);
		$this->assign("ss",$ss);
		$this->assign("type",$type);
		$this->assign("phone",$phone);
		$this->assign("phone_t",$phone_t);
		$this->assign("zr",$zr);
		$this->assign("birth",$birth);
		$this->assign("ugo_start",$ugo_start);
		$this->assign("ugo_end",$ugo_end);
		$this->assign("comment",$comment);
		$this->assign("curr_page",$page);
		
		echo $this->parseTemplate("radnici/browse.tpl");
	}
	//--------------------------------------------
	function editRadnik($id,$page=1,$a,$filter_ime,$filter_tel){
		list($tbl_radnici) = $this->db->select2("SELECT * FROM radnici WHERE radnik_id=$id");
		
		// slika
		$pictures = $this->get_RadnikPictures($tbl_radnici["radnik_ime"],$tbl_radnici["radnik_prezime"]);
		//var_dump($pictures);
		if ($pictures) {
			$this->assign("pictures",implode(";",$pictures));
			$this->assign("picture",$pictures[0]);
		}else{
			//echo "here";
			$this->assign("picture",$this->picturesPath."blank.gif");
		}
		
		foreach ($tbl_radnici as $k=>$v){
			$this->assign($k,$v);
		}
		
		// active
		if($tbl_radnici["radnik_akt"]){
			$this->assign("checked_active"," checked=\"checked\"");
		}else{
			$this->assign("checked_active","");
		}
		
		// ugovor_start
		if ($tbl_radnici["radnik_ugo_start"]>0) {
			$this->assign("ugo_start",date("d.m.Y",$tbl_radnici["radnik_ugo_start"]));
		}
		// ugovor_end
		if ($tbl_radnici["radnik_ugo_end"]>0) {
			$this->assign("ugo_end",date("d.m.Y",$tbl_radnici["radnik_ugo_end"]));
		}
		
		// rodjendan
		$birth = datetime2str($tbl_radnici["radnik_rodjendan"]);
		$this->assign("radnik_rodjendan",$birth);
		
		// grupe
		$tbl_grupe = $this->db->select2("SELECT * FROM radnici_grupe ORDER BY rad_grupa_name");
		if($tbl_grupe[0]){
			foreach ($tbl_grupe as $grupa){
				$grupe_ids[] = ($grupa["rad_grupa_id"] != 0)?$grupa["rad_grupa_id"]:"0 ";
				$grupe_names[] = $grupa["rad_grupa_name"];
				if($grupa["rad_grupa_id"] == $tbl_radnici["radnik_gr_id"]){
					$selected_grupa[] = " selected=\"selected\"";
				}else{
					$selected_grupa[] = "";
				}
			}
			$this->assign("grupa_id",$grupe_ids);
			$this->assign("grupa_name",$grupe_names);
			$this->assign("selected_grupa",$selected_grupa);
		}
		
		// tipovi
		$tbl_tipovi = $this->db->select2("SELECT * FROM radnici_tipovi ORDER BY rad_tip_name");
		if($tbl_tipovi[0]){
			foreach ($tbl_tipovi as $tip){
				$tip_ids[] = ($tip["rad_tip_id"] != 0)?$tip["rad_tip_id"]:"0 ";
				$tip_names[] = $tip["rad_tip_name"];
				if($tip["rad_tip_id"] == $tbl_radnici["radnik_tip_id"]){
					$selected_tip[] = " selected=\"selected\"";
				}else{
					$selected_tip[] = "";
				}
			}
			$this->assign("tip_id",$tip_ids);
			$this->assign("tip_name",$tip_names);
			$this->assign("selected_tip",$selected_tip);
		}
		
		// ZIRO RACUNI
		$tbl_rad_zr = $this->db->select2("SELECT banka_id, ziro FROM radnici_ziro_racuni WHERE rad_id=$id ORDER BY banka_id");
		$tbl_banke = $this->db->select2("SELECT banka_id, banka_name FROM banke ORDER BY banka_name");
		if(!$tbl_rad_zr[0]){
			$tbl_rad_zr[] = array(
				"banka_id" => "",
				"ziro" => ""
			);
		}
		
		foreach ($tbl_rad_zr as $k=>$row){
			$num[] = ($k+1);
			$ziro[] = $row["ziro"];
			foreach ($tbl_banke as $banka){
				$banke_ids[] = $banka["banka_id"];
				$banke_names[] = $banka["banka_name"];
				$select_banka_names[] = "banka_".($k+1);
				if($banka["banka_id"] == $row["banka_id"]){
					$selected_banke[] = " selected=\"selected\"";
				}else{
					$selected_banke[] = "";
				}
			}
			if($k == 0){
				$comm_start[] = "<!--";
				$comm_end[] = "-->";
			}
			$this->assign("banka_id",$banke_ids);
			$this->assign("banka_name",$banke_names);
			$this->assign("select_banka_name",$select_banka_names);
			$this->assign("selected_banka",$selected_banke);
			$sve_banke[] = $this->parseTemplate("sve_banke_select.tpl");
		}
		$this->assign("ziro",$num);
		$this->assign("sve_banke",$sve_banke);
		$this->assign("comment_start_remove_banka",$comm_start);
		$this->assign("comment_end_remove_banka",$comm_end);
		$this->assign("ziro_val",$ziro);
		
		
		// adresa
		$style = array(
			"br" => ' style="clear:both; display:none;"'
		);
		$adresa_html = getAdresaHtml($tbl_radnici["radnik_adresa_id"],"sve","_radnik",$style,2);
		$adresa_lk_html = getAdresaHtml($tbl_radnici["radnik_adresa_lk"],"sve","_radnik_lk",$style,2);
		$this->assign("adresa",$adresa_html);
		$this->assign("adresa_lk",$adresa_lk_html);
		
		// kontakt
		$tbl_tels = $this->db->select2("SELECT * FROM radnici_telefoni WHERE rad_id=".$id." ORDER BY value");
		//print_r($tbl_tels);
		if($tbl_tels[0]){
			foreach ($tbl_tels as $tel){
				if(!$tel["mob"]){
					$tels[] = $tel["value"];
				}else{
					$mobs[] = $tel["value"];
				}
			}
			$this->assign("radnik_tel",@implode("\n",$tels));
			$this->assign("radnik_mob",@implode("\n",$mobs));
		}
		
		// strucna sprema
		$tbl_ss = $this->db->select2("SELECT rad_ss_id, rad_ss_name FROM radnici_sprema ORDER BY rad_ss_name");
		foreach ($tbl_ss as $ss){
			$ss_ids[] = ($ss["rad_ss_id"] != 0)?$ss["rad_ss_id"]:"0 ";
			$ss_names[] = $ss["rad_ss_name"];
			if($ss["rad_ss_id"] == $tbl_radnici["radnik_ssprema_id"]){
				$selected_ss[] = " selected=\"selected\"";
			}else{
				$selected_ss[] = "";
			}
		}
		$this->assign("ss_id",$ss_ids);
		$this->assign("ss_name",$ss_names);
		$this->assign("selected_ss",$selected_ss);
		
		// radni odnos
		$tbl_ro = $this->db->select2("SELECT rad_ro_id, rad_ro_name FROM radnici_ro ORDER BY rad_ro_name");
		foreach ($tbl_ro as $ro){
			$ro_ids[] = ($ro["rad_ro_id"]!=0)?$ro["rad_ro_id"]:"0 ";
			$ro_names[] = $ro["rad_ro_name"];
			if($ro["rad_ro_id"] == $tbl_radnici["radnik_rodnos_id"]){
				$selected_ro[] = " selected=\"selected\"";
			}else{
				$selected_ro[] = "";
			}
		}
		$this->assign("ro_id",$ro_ids);
		$this->assign("ro_name",$ro_names);
		$this->assign("selected_ro",$selected_ro);
		$this->assign("curr_page",$page);
		if(!$a){
			$a="-1";
		}
		$this->assign("active",$a);
		$this->assign("filter_radnik",$filter_ime);
		$this->assign("filter_tel",$filter_tel);
		
		echo $this->parseTemplate("radnici/radnik_edit.tpl");
	}
	//--------------------------------------------
	function deleteRadnik($id){
		$ok = true;
		$q1 = "DELETE FROM radnici WHERE radnik_id=$id";
		$q2 = "DELETE FROM radnici_telefoni WHERE rad_id=$id";
		if((!$this->db->alterTable($q1)) || (!$this->db->alterTable($q2))){
			$ok=false;
		}
		return $ok;
	}
	//--------------------------------------------
	/**
	 * cuva izmene polja o radniku
	 * @param Array $get
	 */
	function saveRadnik($params){
		//echo "<pre>";print_r($params);echo "</pre>";die();
		
		foreach ($params as $k=>$v){
			$params[$k] = trim($v);
		}
		
		$ok = true;
		$match = preg_match('/^\d{2}\.\d{2}\.\d{4}$/',$params["radnik_rodjendan"]);
		if($match){
			$params["radnik_rodjendan"] = str2datetime($params["radnik_rodjendan"]);
		}else{
			$params["radnik_rodjendan"] = "";
		}
		
		// adresa
		if($params["radnik_ulica_id"] && $params["radnik_broj"] && $params["radnik_region"] && $params["radnik_opstina"] && ($params["radnik_grad"] || $params["radnik_lokopstina"] || $params["radnik_naselje"])){
			$adrid = $this->adresa_object->getAddressId($params["radnik_ulica_id"],$params["radnik_broj"],$params["radnik_region"],$params["radnik_opstina"],$params["radnik_grad"],$params["radnik_lokopstina"],$params["radnik_naselje"]);
			if(!$adrid){	// treba da se unese nova adresa
				if($params["radnik_naselje"] > 0){
					$lid = $params["radnik_naselje"];
				}else{
					if($params["radnik_lokopstina"] > 0){
						$lid = $params["radnik_lokopstina"];
					}else{
						$lid = $params["radnik_grad"];
					}
				}
				$this->db->alterTable("INSERT INTO adrese(adresa_ul_id,adresa_lid,adresa_broj) VALUES(".$params["radnik_ulica_id"].",$lid,'".$params["radnik_broj"]."')");
				$adrid = $this->adresa_object->getAddressId($params["radnik_ulica_id"],$params["radnik_broj"],$params["radnik_region"],$params["radnik_opstina"],$params["radnik_grad"],$params["radnik_lokopstina"],$params["radnik_naselje"]);
			}
		}else{
			$adrid=0;
		}
		$params["radnik_adresa_id"] = $adrid;
		
		// adresa LK
		if($params["radnik_ulica_id_lk"] && $params["radnik_broj_lk"] && $params["radnik_region_lk"] && $params["radnik_opstina_lk"] && ($params["radnik_grad_lk"] || $params["radnik_lokopstina_lk"] || $params["radnik_naselje_lk"])){
			$adrid = $this->adresa_object->getAddressId($params["radnik_ulica_id_lk"],$params["radnik_broj_lk"],$params["radnik_region_lk"],$params["radnik_opstina_lk"],$params["radnik_grad_lk"],$params["radnik_lokopstina_lk"],$params["radnik_naselje_lk"]);
			
			if(!$adrid){	// treba da se unese nova adresa
				if($params["radnik_naselje_lk"] > 0){
					$lid = $params["radnik_naselje_lk"];
				}else{
					if($params["radnik_lokopstina_lk"] > 0){
						$lid = $params["radnik_lokopstina_lk"];
					}else{
						$lid = $params["radnik_grad_lk"];
					}
				}
				$this->db->alterTable("INSERT INTO adrese(adresa_ul_id,adresa_lid,adresa_broj) VALUES(".$params["radnik_ulica_id_lk"].",$lid,'".$params["radnik_broj_lk"]."')");
				$adrid = $this->adresa_object->getAddressId($params["radnik_ulica_id_lk"],$params["radnik_broj_lk"],$params["radnik_region_lk"],$params["radnik_opstina_lk"],$params["radnik_grad_lk"],$params["radnik_lokopstina"],$params["radnik_naselje_lk"]);
			}
		}else{
			$adrid = 0;
		}
		$params["radnik_adresa_lk"] = $adrid;
		$params["radnik_komentar"] = utf8RawUrlDecode($params["radnik_komentar"]);
		$params["radnik_komentar"] = utf8RawChrDecode($params["radnik_komentar"]);
		//echo date2int($params["radnik_ugo_start"]);die();
		if(!$params["new"]){
			$q = "UPDATE radnici SET "
				."radnik_gr_id=".$params["radnik_gr_id"].", "
				."radnik_tip_id=".$params["radnik_tip_id"].", "
				."radnik_ugo_start=".date2int($params["radnik_ugo_start"]).", "
				."radnik_ugo_end=".date2int($params["radnik_ugo_end"]).", "
				."radnik_ssprema_id=".$params["radnik_ssprema_id"].", "
				."radnik_rodnos_id=".$params["radnik_rodnos_id"].", "
				."radnik_ime='".$params["radnik_ime"]."', "
				."radnik_prezime='".$params["radnik_prezime"]."', "
				."radnik_adresa_id=".$params["radnik_adresa_id"].", "
				."radnik_adresa_lk=".$params["radnik_adresa_lk"].", "
				."radnik_card_id='".$params["radnik_card_id"]."', "
				."radnik_jmbg='".$params["radnik_jmbg"]."', "
				."radnik_rodjendan='".$params["radnik_rodjendan"]."', "
				."radnik_broj_lk='".$params["radnik_brlk"]."', "
				."radnik_akt=".$params["radnik_akt"].", "
				."radnik_komentar='".$params["radnik_komentar"]."' "
				."WHERE radnik_id=".$params["radnik_id"];
			if(!$this->db->alterTable($q)){
				$ok=false;
			}
		}else{
			$q = "INSERT INTO radnici VALUES("
				."'',"									// ID
				."'".$params["radnik_gr_id"]."',"
				."'".$params["radnik_tip_id"]."',"
				."'".date2int($params["radnik_ugo_start"])."',"
				."'".date2int($params["radnik_ugo_end"])."',"
				."'".$params["radnik_ssprema_id"]."',"
				."'".$params["radnik_rodnos_id"]."',"
				."'".$params["radnik_ime"]."',"
				."'".$params["radnik_prezime"]."',"
				."'',"									// tel
				."'',"									// mob
				."'".$params["radnik_adresa_id"]."',"
				."'".$params["radnik_adresa_lk"]."',"
				."'".$params["radnik_card_id"]."',"
				."'".$params["radnik_jmbg"]."',"
				."'".$params["radnik_rodjendan"]."',"
				."'".$params["radnik_brlk"]."',"
				."'',"									// ZR
				."'',"									// banka
				."'".$params["radnik_akt"]."',"
				."'',"
				."'".$params["radnik_komentar"]."')";
			if(!$this->db->alterTable($q)){
				$ok=false;
			}else{
				$params["radnik_id"] = $this->db->getOne("SELECT LAST_INSERT_ID() FROM radnici");
			}
		}
		//die("$q\n\n");
		
		
		// telefoni
		$this->db->alterTable("DELETE FROM radnici_telefoni WHERE rad_id=".$params["radnik_id"]);
		$tels = explode("{!}",$params["radnik_tel"]);
		$mobs = explode("{!}",$params["radnik_mob"]);
		foreach ($tels as $tel){
			$tel = trim($tel);
			if(strlen($tel)){
				if(!$this->db->alterTable("INSERT INTO radnici_telefoni VALUES(".$params["radnik_id"].",'$tel',0)")){
					$ok=false;
					break;
				}
			}
		}
		// mobilni
		foreach ($mobs as $mob){
			$mob = trim($mob);
			if(strlen($mob)){
				if(!$this->db->alterTable("INSERT INTO radnici_telefoni VALUES(".$params["radnik_id"].",'$mob',1)")){
					$ok=false;
					break;
				}
			}
		}
		
		// ziro racuni
		$this->db->alterTable("DELETE FROM radnici_ziro_racuni WHERE rad_id=".$params["radnik_id"]);
		if(preg_match('/{=}/',$params["radnik_zr"])){
			$items = explode("{!}",$params["radnik_zr"]);
			foreach ($items as $zr){
				$tmp = explode("{=}",$zr);
				//echo "Banka: ".$tmp[0].", ZR: ".$tmp[1]."\n";
				if(!$this->db->alterTable("INSERT INTO radnici_ziro_racuni VALUES(".$params["radnik_id"].",".$tmp[0].",'".$tmp[1]."','')")){
					$ok=false;
					break;
				}
			}
		}
		
		if($ok){
			echo "Izmene sačuvane.";
		}else{
			echo mysql_error();
		}
	}
	function toggleActivity($ids){
		$q = "UPDATE radnici SET radnik_akt=ABS(radnik_akt-1) WHERE ";
		foreach ($ids as $id){
			$q .= "radnik_id=$id OR ";
		}
		$q = substr($q,0,strlen($q)-4);
		//echo $q;
		if($this->db->alterTable($q)){
			echo "Izmene uspešno izvršene";
		}else{
			echo mysql_error();
		}
	}
	
	//--------------------------------------------
	
	/**
	 * GET FRUNCTIONS
	 */
	function get_RadnikPictures($ime,$prezime){
		$ime = strtolower(stripLatins($ime));
		$prezime = strtolower(stripLatins($prezime));
		
		//$image = "$this->picturesPath$ime_$prezime.jpg";
		$ret = array();
		$ok=false;
		if($dir = opendir($this->picturesPath)){
			while ($f = readdir($dir)) {
				if (!preg_match('/^\.+$/',$f)) {
					$test = $ime;
					if (strlen(trim($prezime))) {
						$test .= "_".str_replace(" ","_",$prezime);
					}
					if (preg_match('/^'.$test.'(_\d+)*/',$f)) {
						$ret[] = $this->picturesPath.$f;
						$ok=true;
					}
				}
			}
		}
		
		if ($ok) {
			sort($ret);
			return $ret;
		}else{
			return false;
		}
		
	}
}
?>